// lib/pages/widgets/temp_chart.dart
import 'dart:math' as math;
import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';

/// 封装一条温度曲线
class TempPoint {
  final DateTime time;
  final double value;

  TempPoint(this.time, this.value);
}

class TempChart extends StatelessWidget {
  final List<TempPoint> points;

  const TempChart({
    super.key,
    required this.points,
  });

  @override
  Widget build(BuildContext context) {
    if (points.isEmpty) {
      return const Center(
        child: Text(
          '暂无温度数据',
          style: TextStyle(color: Colors.grey),
        ),
      );
    }

    // 以第一条数据时间为 0 秒
    final baseTime = points.first.time;
    final spots = points.map((p) {
      final sec = p.time.difference(baseTime).inSeconds.toDouble();
      return FlSpot(sec, p.value);
    }).toList();

    // 计算 X/Y 轴范围
    final minX = spots.first.x;
    final maxX = spots.last.x;
    double minY = spots.first.y;
    double maxY = spots.first.y;
    for (final s in spots) {
      minY = math.min(minY, s.y);
      maxY = math.max(maxY, s.y);
    }

    // 稍微留点上下间距
    final dy = (maxY - minY).abs();
    final padding = dy < 5 ? 5.0 : dy * 0.2;
    minY -= padding;
    maxY += padding;

    return LineChart(
      LineChartData(
        minX: minX,
        maxX: maxX,
        minY: minY,
        maxY: maxY,
        backgroundColor: Colors.transparent,
        gridData: FlGridData(
          show: true,
          drawVerticalLine: false,
          getDrawingHorizontalLine: (_) => FlLine(
            color: Colors.white10,
            strokeWidth: 1,
          ),
        ),
        titlesData: FlTitlesData(
          show: true,
          topTitles: AxisTitles(sideTitles: SideTitles(showTitles: false)),
          rightTitles: AxisTitles(sideTitles: SideTitles(showTitles: false)),
          bottomTitles: AxisTitles(
            sideTitles: SideTitles(
              showTitles: true,
              reservedSize: 20,
              getTitlesWidget: (value, meta) {
                // 底部显示 “-Xs”
                final diff = (maxX - value).round();
                return Text(
                  '-${diff}s',
                  style: const TextStyle(color: Colors.white54, fontSize: 10),
                );
              },
            ),
          ),
          leftTitles: AxisTitles(
            sideTitles: SideTitles(
              showTitles: true,
              reservedSize: 32,
              getTitlesWidget: (value, meta) {
                return Text(
                  value.toStringAsFixed(0),
                  style: const TextStyle(color: Colors.white54, fontSize: 10),
                );
              },
            ),
          ),
        ),
        borderData: FlBorderData(
          show: true,
          border: const Border(
            top: BorderSide(color: Colors.white10),
            left: BorderSide(color: Colors.white10),
            right: BorderSide(color: Colors.transparent),
            bottom: BorderSide(color: Colors.white10),
          ),
        ),
        lineTouchData: LineTouchData(
          enabled: true,
          touchTooltipData: LineTouchTooltipData(
            tooltipBgColor: Colors.black87,
            getTooltipItems: (items) {
              return items
                  .map(
                    (e) => LineTooltipItem(
                  '${e.y.toStringAsFixed(1)}°C',
                  const TextStyle(color: Colors.white, fontSize: 12),
                ),
              )
                  .toList();
            },
          ),
        ),
        lineBarsData: [
          LineChartBarData(
            spots: spots,
            isCurved: true,
            barWidth: 3,
            color: Colors.orangeAccent,
            belowBarData: BarAreaData(
              show: true,
              gradient: LinearGradient(
                colors: [
                  Colors.orange.withOpacity(0.3),
                  Colors.transparent,
                ],
                begin: Alignment.topCenter,
                end: Alignment.bottomCenter,
              ),
            ),
            dotData: FlDotData(show: false),
          ),
        ],
      ),
    );
  }
}
